import { Terminal } from './../models/terminal';
import { cloundHttpServer } from './../common/http/http-server';
import { FrontNotifyClient } from "./frontNotifyClient";
import * as guid from 'uuid/v1';

export class CloundService {
    constructor(private cloundHttpServer) {
    }

    private httpResultCallback(result) {
        return new Promise((resolve, reject) => {
            result.then(response => {
                const rData = response.data;
                //老的java接口会这样子返回
                if (typeof rData.code == "undefined") {
                    resolve(rData);
                }
                if (rData.code == 200) resolve(rData.data);
                else reject(rData.message);
            }).catch(error => {
                console.log(error);
                reject(error);
            })
        });
    }

    async  bindTermianl(storeId, name) {
        let url = "/api/stores/" + storeId + "/terminals/bind/device";

        //设备信息的获取,从外壳中读取
        let terminal: Terminal = {
            id: guid(),
            name,
            deviceId: "",
            bound: true,
            boundApp: "dKitchen",
            hardware: "Nexus",
            deviceType: "handheld",
            platform: "browser",
            platformVersion: "4.2",
            userAgent: "userAgent",
            os: "",
            store: { storeId }
        }
        let device = (<any>window).device && (<any>window).device.getDeviceInfo();
        if (device) {
            terminal.deviceId = device.deviceId;
            terminal.deviceType = device.deviceType;
            terminal.hardware = device.hardware;
            terminal.platform = device.platform;
            terminal.platformVersion = device.platformVersion;
        }
        let r = await cloundHttpServer(url).post(terminal);
    }
}

export const devciesService = new CloundService(cloundHttpServer);